package com.logic.landseaserver.ws;

import java.util.HashMap;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.service.IParamConfigService;
import com.logic.landseaserver.ws.dto.ParamConfigDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/paramConfig")
@Api(value = "paramConfig", description = "Parameters Configuration Rest WebServices", position = 6)
public class ParamConfigRest {

	@Autowired
	private IParamConfigService ParamConfigServiceImpl;

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@POST
	@Path("/saveOrUpdate")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "新建或编辑项目第三方参数", position = 1)
	public Response paramConfig(@ApiParam(value = "param", required = true) ParamConfigDTO configDto) {
		CommonResultMap map = new CommonResultMap();
		try {
			String result = ParamConfigServiceImpl.saveOrUpdateParam(configDto);
			map.setData(result);
			map.setResult(CommonResult.SUCCESS, "success");
		} catch (Exception e) {
		    logger.error("paramConfig error.", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
    @POST
    @Path("/getParamByPrjId")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据项目id获取参数信息")
    public Response getParamByPrjId(
            @ApiParam(value = "param", required = true) @QueryParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
        	HashMap<String, Object> result = ParamConfigServiceImpl.getParamByPrjId(id);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS, "success");
        } catch (Exception e) {
            logger.error("getParamByPrjId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
